from django.core.management.base import BaseCommand
from core.models import Advertisement
from django.core.files.base import ContentFile
import os
import shutil
import logging

class Command(BaseCommand):
    help = '创建示例广告数据'

    def handle(self, *args, **kwargs):
        logger = logging.getLogger(__name__)
        try:
            # 确保默认广告图片存在
            default_ad_path = os.path.join('media', 'ads', 'default-ad.jpg')
            if not os.path.exists(default_ad_path):
                self.stdout.write(self.style.WARNING(f'默认广告图片不存在: {default_ad_path}'))
                self.stdout.write(self.style.WARNING('请先运行 python create_default_ad_image.py'))
                return
            
            # 创建示例广告1
            ad1, created = Advertisement.objects.get_or_create(
                title="示例广告1",
                defaults={
                    'description': '<p>这是<strong>示例广告1</strong>的描述，支持<em>富文本</em>格式。</p>',
                    'url': 'https://example.com/ad1',
                    'is_active': True,
                    'display_order': 1,
                }
            )
            
            # 只有在新创建时才设置图片
            if created:
                # 复制默认广告图片并设置为广告图片
                sample_ad_path = os.path.join('media', 'ads', 'sample-ad1.jpg')
                shutil.copy(default_ad_path, sample_ad_path)
                ad1.image.name = 'ads/sample-ad1.jpg'
                ad1.save()
                self.stdout.write(self.style.SUCCESS(f'成功创建示例广告1: {ad1.title}'))
            else:
                self.stdout.write(self.style.SUCCESS(f'示例广告1已存在: {ad1.title}'))
            
            # 创建示例广告2
            ad2, created = Advertisement.objects.get_or_create(
                title="示例广告2",
                defaults={
                    'description': '<p>这是<strong>示例广告2</strong>的描述。</p><p>可以包含<span style="color:blue;">多段落</span>和<span style="color:red;">颜色</span>。</p>',
                    'url': 'https://example.com/ad2',
                    'is_active': True,
                    'display_order': 2,
                }
            )
            
            # 只有在新创建时才设置图片
            if created:
                # 复制默认广告图片并设置为广告图片
                sample_ad_path = os.path.join('media', 'ads', 'sample-ad2.jpg')
                shutil.copy(default_ad_path, sample_ad_path)
                ad2.image.name = 'ads/sample-ad2.jpg'
                ad2.save()
                self.stdout.write(self.style.SUCCESS(f'成功创建示例广告2: {ad2.title}'))
            else:
                self.stdout.write(self.style.SUCCESS(f'示例广告2已存在: {ad2.title}'))
            
            # 创建示例广告3
            ad3, created = Advertisement.objects.get_or_create(
                title="示例广告3",
                defaults={
                    'description': '<p>这是示例广告3的描述。</p><ul><li>支持列表</li><li>和其他格式</li></ul>',
                    'url': 'https://example.com/ad3',
                    'is_active': True,
                    'display_order': 3,
                }
            )
            
            # 只有在新创建时才设置图片
            if created:
                # 复制默认广告图片并设置为广告图片
                sample_ad_path = os.path.join('media', 'ads', 'sample-ad3.jpg')
                shutil.copy(default_ad_path, sample_ad_path)
                ad3.image.name = 'ads/sample-ad3.jpg'
                ad3.save()
                self.stdout.write(self.style.SUCCESS(f'成功创建示例广告3: {ad3.title}'))
            else:
                self.stdout.write(self.style.SUCCESS(f'示例广告3已存在: {ad3.title}'))
            
            # 创建示例广告4
            ad4, created = Advertisement.objects.get_or_create(
                title="示例广告4",
                defaults={
                    'description': '<p>这是示例广告4的描述。</p><p>点击查看详情！</p>',
                    'url': 'https://example.com/ad4',
                    'is_active': True,
                    'display_order': 4,
                }
            )
            
            # 只有在新创建时才设置图片
            if created:
                # 复制默认广告图片并设置为广告图片
                sample_ad_path = os.path.join('media', 'ads', 'sample-ad4.jpg')
                shutil.copy(default_ad_path, sample_ad_path)
                ad4.image.name = 'ads/sample-ad4.jpg'
                ad4.save()
                self.stdout.write(self.style.SUCCESS(f'成功创建示例广告4: {ad4.title}'))
            else:
                self.stdout.write(self.style.SUCCESS(f'示例广告4已存在: {ad4.title}'))
            
            self.stdout.write(self.style.SUCCESS('示例广告数据创建完成！'))
            
        except Exception as e:
            logger.error(f"创建示例广告数据时出错: {e}", exc_info=True)
            self.stdout.write(self.style.ERROR(f'创建示例广告数据时出错: {e}')) 